package com.bw.zg3_day05zy.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zg3_day05zy.R
import com.bw.zg3_day05zy.databinding.ActivityLoginBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class LoginActivity : BaseActivity<ActivityLoginBinding, UserViewModel>() {

    override fun initView() {
        binding.btn.setOnClickListener {
            val uname = binding.etUname.text.toString()
            val pwd = binding.etPwd.text.toString()
            if (uname.isNullOrEmpty() || pwd.isNullOrEmpty()){
                ToastUtils.showLong("用户名或密码不能为空")
                return@setOnClickListener
            }
            viewModel.login(pwd,uname)
        }
    }

    override fun initData() {
        viewModel.loginData.observe(this){
            ARouter.getInstance().build("/app/MainActivity").navigation()
            SPUtils.getInstance().apply {
                put("token",it.token)
                put("uid",it.id)
                put("role",it.roleid)
            }
        }
    }
}